#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
void test1();
void test2();
void test3();
void test4();
void buttle(void* p1, int count, int size, int(*cmp)(void*, void*));
int main() {
	test1();
	printf("\n");
	test2();
	printf("\n");
	test3();
	printf("\n");
	test4();
	return 0;
}
struct Stu
{
	char name[20];
	int age;
};
int int_cmp(const void *p1,const void *p2) {
	return (*(int*)p1 - *(int*)p2);
}
int char_cmp(const void* p1, const void* p2) {
	return (*(char*)p1 - *(char*)p2);
}
int struct_name_cmp(const void* p1, const void* p2) {
	return strcmp(((struct Stu*)p1)->name,((struct Stu*)p2)->name);
}
int struct_age_cmp(const void* p1, const void* p2) {
	return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
}
void test1() {
	int arr1[10] = { 0,9,8,3,4,5,6,7,1,2 };
	int i = 0;
	//qsort(arr1,sizeof(arr1)/sizeof(arr1[0]),sizeof(int),int_cmp);
	buttle(arr1, sizeof(arr1) / sizeof(arr1[0]), sizeof(int), int_cmp);
	for ( i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}
void test2() {
	char arr2[10] = { 's','a','q','y','o','p','u','t','x','b'};
	int i = 0;
	//qsort(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(char), char_cmp);
	buttle(arr2, sizeof(arr2) / sizeof(arr2[0]), sizeof(char), char_cmp);
	for (i = 0; i < sizeof(arr2) / sizeof(arr2[0]); i++)
	{
		printf("%c ", arr2[i]);
	}
	return 0;
}

void test3() {
	struct Stu arr3[] = { {"zhangsan",20},{"lisi",18},{"wangwu",19}};
	int i = 0;
	//qsort(arr3, sizeof(arr3) / sizeof(arr3[0]), sizeof(arr3[0]), struct_name_cmp);
	buttle(arr3, sizeof(arr3) / sizeof(arr3[0]), sizeof(arr3[0]), struct_name_cmp);
	for (i = 0; i < sizeof(arr3) / sizeof(arr3[0]); i++)
	{
		printf("%s,%d ",arr3[i].name,arr3[i].age);
	}
	return 0;
}
void test4() {
	struct Stu arr4[] = { {"zhangsan",20},{"lisi",18},{"wangwu",90} };
	int i = 0;
	//qsort(arr4, sizeof(arr4) / sizeof(arr4[0]), sizeof(arr4[0]), struct_age_cmp);
	buttle(arr4, sizeof(arr4) / sizeof(arr4[0]), sizeof(arr4[0]), struct_age_cmp);
	for (i = 0; i < sizeof(arr4) / sizeof(arr4[0]); i++)
	{
		printf("%s,%d ", arr4[i].name, arr4[i].age);
	}
	return 0;
}
void swap(void *p1, void *p2, int size) {
	int i = 0;
	for ( i = 0; i < size; i++)
	{
		char temp = *((char *)p1 + i);
		*((char *)p1 + i) = *((char *)p2 + i);
		*((char*)p2 + i) = temp;
	}
}
void buttle(void* p1, int count, int size, int(*cmp)(void*, void*)) {
	int i = 0;
	int j = 0;
	for ( i = 0; i < count-1; i++)
	{
		for (j = 0; j < count - 1 - i; j++) {
			if (cmp((char *)p1+j*size, (char *)p1 + (j+1) * size)>0)
			{
				swap((char *)p1 + j * size, (char *)p1 + (j + 1) * size, size);
			}
		}
	}
}